package com.cmict.data.bpm.controller.admin.externalfile;

import com.cmict.data.bpm.controller.admin.externalfile.vo.ExternalFileCreateReqVO;
import com.cmict.data.bpm.controller.admin.externalfile.vo.ExternalFilePageReqVO;
import com.cmict.data.bpm.controller.admin.externalfile.vo.ExternalFileRespVO;
import com.cmict.data.bpm.controller.admin.externalfile.vo.ExternalFileUpdateReqVO;
import com.cmict.data.bpm.convert.externalfile.ExternalFileConvert;
import com.cmict.data.bpm.dal.dataobject.externalfile.ExternalFileDO;
import com.cmict.data.bpm.service.externalfile.ExternalFileService;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.pojo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static com.cmict.data.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 - 工作流审批附件")
@RestController
@RequestMapping("/bpm/external-file")
@Validated
public class ExternalFileController {

    @Resource
    private ExternalFileService externalFileService;

    @PostMapping("/create")
    @ApiOperation("创建工作流审批附件")
    @PreAuthorize("@ss.hasPermission('bpm:external-file:create')")
    public CommonResult<Long> createExternalFile(@Valid @RequestBody ExternalFileCreateReqVO createReqVO) {
        return success(externalFileService.createExternalFile(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新工作流审批附件")
    @PreAuthorize("@ss.hasPermission('bpm:external-file:update')")
    public CommonResult<Boolean> updateExternalFile(@Valid @RequestBody ExternalFileUpdateReqVO updateReqVO) {
        externalFileService.updateExternalFile(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除工作流审批附件")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('bpm:external-file:delete')")
    public CommonResult<Boolean> deleteExternalFile(@RequestParam("id") Long id) {
        externalFileService.deleteExternalFile(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得工作流审批附件")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('bpm:external-file:query')")
    public CommonResult<ExternalFileRespVO> getExternalFile(@RequestParam("id") Long id) {
        ExternalFileDO externalFile = externalFileService.getExternalFile(id);
        return success(ExternalFileConvert.INSTANCE.convert(externalFile));
    }

    @GetMapping("/list")
    @ApiOperation("获得工作流审批附件列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('bpm:external-file:query')")
    public CommonResult<List<ExternalFileRespVO>> getExternalFileList(@RequestParam("ids") Collection<Long> ids) {
        List<ExternalFileDO> list = externalFileService.getExternalFileList(ids);
        return success(ExternalFileConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得工作流审批附件分页")
    @PreAuthorize("@ss.hasPermission('bpm:external-file:query')")
    public CommonResult<PageResult<ExternalFileRespVO>> getExternalFilePage(@Valid ExternalFilePageReqVO pageVO) {
        PageResult<ExternalFileDO> pageResult = externalFileService.getExternalFilePage(pageVO);
        return success(ExternalFileConvert.INSTANCE.convertPage(pageResult));
    }

//    @GetMapping("/export-excel")
//    @ApiOperation("导出工作流审批附件 Excel")
//    @PreAuthorize("@ss.hasPermission('bpm:external-file:export')")
//    @OperateLog(type = EXPORT)
//    public void exportExternalFileExcel(@Valid ExternalFileExportReqVO exportReqVO,
//              HttpServletResponse response) throws IOException {
//        List<ExternalFileDO> list = externalFileService.getExternalFileList(exportReqVO);
//        // 导出 Excel
//        List<ExternalFileExcelVO> datas = ExternalFileConvert.INSTANCE.convertList02(list);
//        ExcelUtils.write(response, "工作流审批附件.xls", "数据", ExternalFileExcelVO.class, datas);
//    }

}
